<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增产品入库</title>

    <script src="js/jquery-1.10.2.min.js"></script>

</head>
<body>
<p>
    产品编号:<input type="text" id="pno" name="pno" placeholder="产品编号"/>
</p>
<p>
    产品名:<input type="text" id="pname" name="pname" placeholder="产品名"/>
</p>
<p>
    生产地址:<input type="text" id="paddr" name="paddr" placeholder="产品生产地址"/>
</p>
<p>
    生产日期:<input type="text" id="pdate" name="pdate" placeholder="产品生产日期"/>
</p>
<p>
    入库日期:<input type="text" id="enterdate" name="enterdate" placeholder="入库日期"/>
</p>
<p>
    入库数量:<input type="text" id="pnumber" name="pnumber" placeholder="入库数量"/>
</p>
<p>
    <input type="button" id="commit" name="commit" value="新增入库"/>
</p>

<script type="text/javascript">
    //1、监听btn事件
    var btn = document.getElementById("commit");
    btn.onclick = function () {
        //点击按钮时，代码到此处执行
        //1、获取 发起请求的数据
        var pno = document.getElementById("pno").value;
        var pname = document.getElementById("pname").value;
        var paddr = document.getElementById("paddr").value;
        var pdate = document.getElementById("pdate").value;
        var enterdate = document.getElementById("enterdate").value;
        var pnumber = document.getElementById("pnumber").value;

        //json对象
        var reqData = {
            "pno":pno,
            "pname":pname,
            "paddr":paddr,
            "pdate":pdate,
            "enterdate":enterdate,
            "pnumber":pnumber
        };
        //2、构建请求的参数
        var config = {
            method: "POST",
            url: "/java/product",
            dataType: "json",
            contentType:"application/json",
            data: JSON.stringify(reqData),
            success: function (resp) {
                //接受服务器返回的数据并处理
                console.log(resp);
                if(resp.code === 2101){
                    window.location.href = "index.html";
                }else if(resp.code === 2102){
                    alert(resp.msg);
                }
            }
        }
        //3、发起请求
        $.ajax(config);
    }

</script>
</body>
</html>